const api    = require('../api/api');
const logger = require('../util/log');
const Const  = require('../common/const');
const config = require('../crawler/config/config');
const decode = require('urldecode');

module.exports = function (app) {
    app.get('/',function (req,res) {
        res.send('Hello Node Js...');
    });

    /**
     * 全局功能路由
     */
    app.get('/do',async (req,res) => {
        let param = req.query;
        let rows;
        try {
            param.path = decode(decode(param.path));
            param.cookie = decode(param.cookie);
            logger.info(`Execute Params:${JSON.stringify(param)}`);
            switch (param.type) {
                case config.CRAWLER.MERCHANT_WORDS.CS_SEARCH_KEYWORD.NAME:
                    rows = await api.searchKey(param.path,param.cookie);
                    break;
                case config.CRAWLER.MERCHANT_WORDS.CS_DETAIL_KEYWORD.NAME:
                    rows = await api.keywordSearchDetail(param.path,param.cookie);
                    break;
                case config.CRAWLER.MERCHANT_WORDS.ET_SEARCH.NAME:
                    rows = await api.etSearchList(param.path,param.cookie);
                    break;
                case config.CRAWLER.MERCHANT_WORDS.P1_SEARCH.NAME:
                    rows = await api.p1SearchList(param.path,param.cookie);
                    break;
                case config.CRAWLER.MERCHANT_WORDS.A_PLUSH_SEARCH.NAME:
                    rows = await api.aplushSearchList(param.path,param.cookie);
                    break;
                case config.CRAWLER.MERCHANT_WORDS.ET_KEYWORD_SEARCH.NAME:
                    rows = await api.etSearchKeyword(param.path,param.cookie,);
                    break;
                default:
                    logger.err(`Route not matched. Params:${JSON.stringify(param)}`);
                    await res.json(resError(`Route not matched`));
                    break
            }
            await res.json(resSuccess(rows));
        }catch (e) {
            e.stack;
            logger.err(`Process Request Url:[/do] Failed!!!. \n Params:${JSON.stringify(param)}  Message:${e.message}`);
            await res.json(resError(e.message));
        }finally {
            res.end();
        }
    });

    /**
     * 魔词
     * CS 搜索关键字列表
     */
    app.get('/cs_search_keyword',async (req,res) => {
        try {
            let searchUrl = req.query.url;
            let rows = await api.searchKey(searchUrl);
            await res.json(resSuccess(rows));
        } catch (e) {
            logger.err(`Process Request Url:[/cs_search_keyword] Failed!!!.  Message:${e.message}`);
            await res.json(resError(e.message))
        }finally {
            res.end();
        }
    });

    /**
     * 魔词
     * CS 关键字详情
     */
    app.get('/cs_keyword_search_detail',async (req,res) => {
        try {
            let queryVo = req.query.url;
            let rows = await api.keywordSearchDetail(queryVo);
            await res.json(resSuccess(rows));
        } catch (e) {
            logger.err(`Process Request Url:[/cs_keyword_search_detail] Failed!!!.  Message:${e.message}`);
            await res.json(resError(e.message))
        }finally {
            res.end();
        }
    });

    /**
     * et列表查询
     */
    app.get('/et_search_list',async (req,res) => {
        try {
            let searchUrl = req.query.url;
            let rows = await api.etSearchList(searchUrl);
            await res.json(resSuccess(rows));
        } catch (e) {
            logger.err(`Process Request Url:[/et_search_list] Failed!!!.  Message:${e.message}`);
            await res.json(resError(e.message))
        }finally {
            res.end();
        }
    });

    /**
     * 获取P1搜索数据
     */
    app.get('/p1_search_keyword',async (req,res) => {
        try {
            let searchUrl = req.query.url;
            let rows = await api.p1SearchList(searchUrl);
            await res.json(resSuccess(rows));
        } catch (e) {
            logger.err(`Process Request Url:[/p1_search_keyword] Failed!!!.  Message:${e.message}`);
            await res.json(resError(e.message))
        }finally {
            res.end();
        }
    });

    /**
     * 获取A+数据
     */
    app.get('/a_plush_search_keyword',async (req,res) => {
        try {
            let searchUrl = req.query.url;
            let rows = await api.aplushSearchList(searchUrl);
            await res.json(resSuccess(rows));
        } catch (e) {
            logger.err(`Process Request Url:[/a_plush_search_keyword] Failed!!!.  Message:${e.message}`);
            await res.json(resError(e.message))
        }finally {
            res.end();
        }
    });

    function resError(message) {
        return {
            code:Const._RETCODE_HANDLER_FAILED,
            message:message,
            data:{
                rows:[],
                code:Const._RETCODE_HANDLER_FAILED
            }
        }
    }

    function resSuccess(rows = []) {
        return {
            code:Const._RETCODE_SUCCESS,
            message:'Success',
            data:rows
        }
    }

};
